package com.example.selenium.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.selenium.common.Result;
import com.example.selenium.queue.rabbitmq.f.MessageInfo;
import com.example.selenium.service.RabbitMqListenerService;
import com.example.selenium.util.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * @Author : 刘思盖
 * @Date : 2021/9/26 12:01
 */
@Service
@Slf4j
public class RabbitMqListenerServiceImpl implements RabbitMqListenerService {


    /**
     * 监听运单列表
     */
    @Override
    public void handleWaybillNoList() {

    }

    /**
     * 处理消息
     * @param message
     * @return
     */
    @Override
    public Result handleWaybillDetail(MessageInfo message) {
        return write("waybillInfo", message);
    }

    @Override
    public Result handlePayInfoDetail(MessageInfo message) {
        return write("payInfo", message);
    }

    /**
     * 写入文件
     * @param fileType
     * @param message
     * @return
     */
    private Result write(String fileType,MessageInfo message){
        String waybillNo = message.getWaybillNo();
        String directory = FileUtils.createDirectory(waybillNo,fileType);
        try{
            FileUtils.witeInTxt(directory,waybillNo,message.getData());
            return Result.success();
        }catch (Exception e){
            log.error("写入文件失败，运单号："+waybillNo);
            e.printStackTrace();
            return Result.error();
        }
    }
}
